/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.tienlen.handler;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import extension.game.room.RoomExtension;
import extension.room.tienlen.TienlenGame;
import extension.room.tienlen.TienlenPlayer;
import extension.room.tienlen.TienlenResponse;
import java.util.List;

/**
 *
 * @author daz
 */
public class TimeOutHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        RoomExtension tienlenExtension = (RoomExtension) getParentExtension();
        TienlenGame game = (TienlenGame) tienlenExtension.getGame();
        TienlenPlayer p = game.getPlayerFight();

        if (p.getName().equals(user.getName())) {
            if (game.getLastBlock() == -1) {
                List<Integer> listCards = game.getAutoFightCard();

                game.removeCardInHand(listCards);
                List<User> listSend = tienlenExtension.getParentRoom().getUserList();
                TienlenResponse response = (TienlenResponse) tienlenExtension.getResponse();
                response.sendFightCard(listCards, game.getGameTurn(), 1, listSend);
                //check remain card
                if (p.getNumberCardInHands() <= 0) {
                    int rank = game.playerFinish();
                    response.sendFightAll(rank, p.getSeatPos(), game.getNumbeView());
                }
                if (!game.getIsFinishMatch()) {
                    game.nextTurn();
                } else {
                    game.onFinishMatch();
                }
            } else {
                game.skipTurn(p.getSeatPos());
            }
        }
    }
}
